//
//  DynamicViewController.m
//  LearnObjectiveC
//
//  Created by 吴可高 on 16/9/23.
//  Copyright © 2016年 吴可高. All rights reserved.
#import <objc/runtime.h>

@interface DynamicItemView : UIView<UIDynamicAnimatorDelegate,UIDynamicItem>
@property (strong, nonatomic) UIColor * backColor;

@end

@implementation DynamicItemView

-(void)setBackColor:(UIColor *)backColor
{
    objc_setAssociatedObject(self, @selector(setBackgroundColor:), backColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(UIColor *)backColor
{
   return objc_getAssociatedObject(self, @selector(setBackgroundColor:));
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frame = CGRectMake(CGRectGetMinX(frame), CGRectGetMinY(frame), 80, 80);
        self.backgroundColor = self.backColor == nil ?  [UIColor lightGrayColor] : self.self.backColor;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 40.f;
    }
    return self;
}
#pragma mark UIDynamicAnimatorDelegate
- (void)dynamicAnimatorWillResume:(UIDynamicAnimator *)animator;
{
}
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator *)animator;
{

}
#pragma mark UIDynamicItem
- (void)willMoveToAnimator:(nullable UIDynamicAnimator *)dynamicAnimator;
{

}
@end

@interface GravityBehaviorView : UIView
@property (strong, nonatomic) DynamicItemView * indicatorView;
@property (strong, nonatomic) DynamicItemView * indicatorView1;

@end
@interface GravityBehaviorView ()
@property (strong, nonatomic) UIDynamicAnimator * animator;
@property (strong, nonatomic) UIGravityBehavior * gravityBehavior;
@property (strong, nonatomic) UICollisionBehavior * collisionBehavior;
@property (strong, nonatomic) UISnapBehavior * snapeBehavior;

@end

@implementation GravityBehaviorView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.indicatorView];
        UISnapBehavior * snapeBehavior =  [[UISnapBehavior alloc] initWithItem:self.indicatorView snapToPoint:CGPointMake(10, 10)];
        snapeBehavior.damping = 1.f;
        [self.animator addBehavior:snapeBehavior];
        
        /*
        [self addSubview:self.indicatorView1];
        self.gravityBehavior.magnitude = 1.f;
        [self.gravityBehavior addItem:self.indicatorView];
        [self.gravityBehavior addItem:self.indicatorView1];

        [self.collisionBehavior addItem:self.indicatorView];
        [self.collisionBehavior addItem:self.indicatorView1];
        [self.animator addBehavior:self.gravityBehavior];
        [self.animator addBehavior:self.collisionBehavior];
        
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
                              CGRectMake(0,150, self.frame.size.width -40, self.frame.size.width -40)];
        [self.collisionBehavior addBoundaryWithIdentifier:@"circle" forPath:path];
        */

     
    }
    return self;
}
#pragma mark View Algorithm Processor
#pragma mark Setter Method View Style
#pragma mark Getter Method View Style

-(DynamicItemView *)indicatorView
{
    _indicatorView = ({
        if (!_indicatorView) {
            _indicatorView  = [[DynamicItemView alloc]initWithFrame:CGRectMake(50, 0, 100, 100)];
            _indicatorView.backgroundColor = [UIColor redColor];
        }
        _indicatorView;
    });
    return _indicatorView;
}

-(DynamicItemView *)indicatorView1
{
    _indicatorView1 = ({
        if (!_indicatorView1) {
            _indicatorView1  = [[DynamicItemView alloc]initWithFrame:CGRectMake(50,250, 100, 100)];
            _indicatorView1.backColor = [UIColor grayColor];
        }
        _indicatorView1;
    });
    return _indicatorView1;
}

-(UIDynamicAnimator *)animator
{
    //创建一个物理仿真器
    _animator = ({
        if (!_animator) {
            _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self];
        }
        _animator;
    });
    return _animator;
}
-(UIGravityBehavior *)gravityBehavior
{
    _gravityBehavior = ({
        if (!_gravityBehavior) {
            _gravityBehavior = [[UIGravityBehavior alloc]init];
        }
        _gravityBehavior;
    });
    return _gravityBehavior;
}

-(UICollisionBehavior*)collisionBehavior
{
    _collisionBehavior =({
        if (!_collisionBehavior) {
            _collisionBehavior  = [[UICollisionBehavior alloc]init];
            _collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
        }
        _collisionBehavior;
    });
    return _collisionBehavior;
}

-(UISnapBehavior *)snapeBehavior
{
    _snapeBehavior  = ({
        if (!_snapeBehavior) {
            _snapeBehavior  = [[UISnapBehavior alloc]init];
        }
        _snapeBehavior;
    });
    return _snapeBehavior;
}
#pragma mark IBOutlet Action View

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.获得手指对应的触摸对象
    UITouch *touch = [touches anyObject];
    // 2.获得触摸点
    CGPoint point = [touch locationInView:self];
    // 3.创建捕捉行为
    UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.indicatorView snapToPoint:point];
    // 防震系数，damping越大，振幅越小
    snap.damping = 1;
    // 4.清空之前的并再次开始
    [self.animator removeAllBehaviors];
    [self.animator addBehavior:snap];
  
}
@end
//

@interface CollisionBehaviorView : UIView
@end
@implementation CollisionBehaviorView
@end
//
@interface SnapeBehaviorView : UIView
@end
@implementation SnapeBehaviorView
@end
//
@interface PushBehaviorView : UIView
@end
@implementation PushBehaviorView
@end
//
@interface AttachmentBehaviorView : UIView
@end
@implementation AttachmentBehaviorView
@end


#import "DynamicViewController.h"

@interface DynamicViewController ()
@property (strong, nonatomic) GravityBehaviorView * gravityBehaviorView;
@property (strong, nonatomic) CollisionBehaviorView * collisioBehaviorView;
@property (strong, nonatomic) SnapeBehaviorView * snapeBehaviorView;
@property (strong, nonatomic) PushBehaviorView * pushBehaviorView;
@property (strong, nonatomic) AttachmentBehaviorView * attachmentBehaviorView;
@end

@implementation DynamicViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    [self setObjectView];

}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}
#pragma mark ViewController AsynRequestData
-(void)accountInfoAsynRequest;
{

}

#pragma mark ViewController Algorithm Processor

#pragma mark ViewController Initialize SubObjectView
-(void)setObjectView
{
    __weak  typeof(self)weakSelf = self;
    [self.view addSubview:self.gravityBehaviorView];
    [self.gravityBehaviorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.view.mas_top).with.offset(0.f);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).with.offset(-49.f);
    }];
}

#pragma mark --viewController getter methods
-(GravityBehaviorView *)gravityBehaviorView
{
    _gravityBehaviorView = ({
        if (!_gravityBehaviorView) {
            _gravityBehaviorView  = [[
            GravityBehaviorView alloc]initWithFrame:CGRectZero];
        }
        _gravityBehaviorView;
    });
    return _gravityBehaviorView;
}

-(CollisionBehaviorView *)collisioBehaviorView
{
    _collisioBehaviorView = ({
        if (!_collisioBehaviorView) {
            _collisioBehaviorView  = [[CollisionBehaviorView alloc]initWithFrame:CGRectZero];
        }
        _collisioBehaviorView;
    });
    return _collisioBehaviorView;
}

-(SnapeBehaviorView *)snapeBehaviorView
{
    _snapeBehaviorView = ({
        _snapeBehaviorView = [[SnapeBehaviorView alloc]initWithFrame:CGRectZero];
    });
    return _snapeBehaviorView;
}
-(PushBehaviorView *)pushBehaviorView
{
    _pushBehaviorView = ({
        if (!_pushBehaviorView) {
            _pushBehaviorView  = [[PushBehaviorView alloc]initWithFrame:CGRectZero];
        }
        _pushBehaviorView;
    });
    return _pushBehaviorView;
}

-(AttachmentBehaviorView *)attachmentBehaviorView
{
    _attachmentBehaviorView = ({
        if (!_attachmentBehaviorView) {
            _attachmentBehaviorView  = [[AttachmentBehaviorView alloc]initWithFrame:CGRectZero];
        }
        _attachmentBehaviorView;
    });
    return _attachmentBehaviorView;
}
#pragma mark Possess  Delegate ViewController（委托代理者）
#pragma mark IBOutlet Action ViewController

#pragma mark Other Method

@end
